home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: newshub.nosc.mil!news!sampson
- From: sampson@nosc.mil (Charles H. Sampson)
- Subject: Re: Specifying Data Layout
- Message-ID: <1996Mar21.171237.7459@nosc.mil>
- Sender: news@nosc.mil
- Organization: Computer Sciences Corporation
- References: <1996Mar14.173643.8651@nosc.mil> <VA.0000006c.00028969@fred>
- Date: Thu, 21 Mar 1996 17:12:37 GMT
-
- In article <VA.0000006c.00028969@fred>,
- Frederic LACHASSE <lachass@worldnet.fr> wrote:
- >In article <1996Mar14.173643.8651@nosc.mil>, sampson@cod.nosc.mil (mE) wrote:
- >>
- >> Is there some way of precisely specifying the memory layout of a
- >> struct? In other words, can you say, "This struct is to occupy 3 words of
- >> memory, element foo is to occupy bits 5-12 of word 1, element fern is to
- >> occupy ..."? If it can be done with a struct, can it also be done with a
- >> class (specifying locations for the data members only)? In this case, can
- >> you also control the gizmo that is used to distinguish the various values
- >> of a class hierarchy, both where it is to be allocated and the values to be
- >> used to identify specific members of the hierarchy?
- >>
- >The way to do it is:
- >
- >struct Packed
- >{
- > unsigned short a:3; // 3 bits
- > unsigned short b:5; // 5 more bits
- > unsigned short c:8; // the remaining 8 bits for a 16 bits unsigned short
- >};
- >
- >The a, b, c member of the Packed struct will be stored in 2 bytes of memory.
- >
- >BTW, the same syntax works for classes.
- >
- >The exact layout of the bits is implementation dependant as:
- > - unsigned short isn't necessarily 16 bits long,
- > - the processor store most significant bytes first or last.
- >So read compiler documentation and experiment to find out which exact layout
- >will work for you.
- >
- >One reason you've found nothing in the C++ FAQ is that it is a C syntax that
- >C++ have inherited. As most C++ programmers learn C first, they already know
- >this trick.
-
- I appreciate your response. However, after reading it and two others
- that contained essentially the same information, it seems that the answer
- to my question is "No". Finding out what your particular compiler does is
- not at all the same thing as specifying what any compiler is to do.
-
- It's not clear what "trick" you're referring to. Bit-fields appear in
- Stroustrup, but basically he says that except for specifying the length of
- the component, everything is up for grabs. I don't personally consider
- reading the compiler manual a trick, but based on my experience as some-
- thing like a consultant, apparently many people do. :-)
-
- By the way, none of the respondents have addressed the issue of con-
- trolling a classes "tag". My guess is that it is impossible to either
- allocate it or specify its values. (Neither can be done in Ada either.)
-
- (My apologies for leaving in so much quoted material, but I couldn't
- figure out how to cut much and have what was left make any sense.)
-
- Charlie
-